<html xmlns="http://www.w3.org/1999/xhtml"><head><style type="text/css">
dt {
  font-style: italic;
  margin-top: 15px;
  margin-bottom: 3px;
  margin-left: 0px;
  border-bottom: 1px dotted black;
}
dd {
  margin-left: 10px;
}
table {
  border-collapse:collapse;
  border: 1px solid black;
  margin-top: 7px;
}
th {
  border: 1px solid black;
  padding: 3 7 3 7;
}
td {
  border: 1px solid black;
  padding: 3 7 3 7;
}
</style></head><body><p><a name="invokevirtual"></a><b>invokevirtual</b> :
  call an instance method : <a href="opcodes.html">index</a> : <a href="http://asm.objectweb.org/asm40/javadoc/user/org/objectweb/asm/MethodVisitor.html#visitMethodInsn(int,%20java.lang.String,%20java.lang.String,%20java.lang.String)">visitMethodInsn()</a></p><dl><dt>Description</dt><dd>invokevirtual dispatches a Java method. It is used in Java to
invoke all methods except interface methods (which use
invokeinterface), static methods (which use invokestatic),
and the few special cases handled by invokespecial. <br></br>For example, when you write in Java:<br></br><pre>
    Object x;
    ...
    x.equals("hello");
</pre>
this
is compiled into something like:<br></br><pre>
    aload_1       ; push local variable 1 (i.e. 'x') onto stack
    ldc "hello"   ; push the string "hello" onto stack
    ; invoke the equals method
    invokevirtual java/lang/Object/equals(Ljava/lang/Object;)Z
    ; the boolean result is now on the stack
</pre>
Note
that the actual method run depends on the runtime type of the object
invokevirtual is used with. So in the example above, if x is an
instance of a class that overrides Object's equal method, then the subclasses'
overridden version of the equals method will be used.<br></br>Before performing the method invokation, the class and the method identified by
&lt;method-spec&gt; are resolved. See Chapter 9 for a description of how
methods are resolved.<br></br>invokevirtual looks at the <i xmlns="">descriptor</i> given in
&lt;method-spec&gt;, and determines how many arguments the method takes (this
may be zero). It pops these arguments off the operand stack. Next it pops
<i xmlns="">objectref</i> off the stack. <i xmlns="">objectref</i> is a reference to the object
whose method is being called. invokevirtual retrieves the Java class
for <i xmlns="">objectref</i>, and searches the list of methods defined by that class
and then its superclasses, looking for a method called <i xmlns="">methodname,</i> whose
descriptor is <i xmlns="">descriptor</i>.<br></br>Once a method has been located, invokevirtual calls the method. First,
if the method is marked as synchronized, the monitor associated with
<i xmlns="">objectref</i> is entered. Next, a new stack frame structure is established
on the call stack. Then the arguments for the method (which were popped off the
current method's operand stack) are placed in local variables of the new stack
frame structure. arg1 is stored in local variable 1, arg2 is stored in local
variable 2 and so on. <i xmlns="">objectref</i> is stored in local variable 0 (the local
variable used for the special Java variable this). Finally, execution
continues at the first instruction in the bytecode of the new method.  <br></br>When the method called by invokevirtual returns, any single (or
double) word return result is placed on the operand stack of the
current method and execution continues at the instruction that follows
invokevirtual in the bytecode.</dd><dt>Exceptions</dt><dd>NullPointerException - <i xmlns="">objectref</i> is null<br></br>StackOverflowError - no more space in callstack for a new stack frame</dd><dt>See also</dt><dd><a href="ref-invokespecial.html">invokespecial</a> <a href="ref-.html"></a> <a href="ref-invokestatic.html">invokestatic</a> <a href="ref-.html"></a> <a href="ref-invokeinterface.html">invokeinterface</a> </dd><dt>Stack</dt><dd><table xmlns="">
<tr>
<td>
<b>Before </b>
</td>
<td><b>After</b></td></tr>
<tr>
<td>arg1</td>
<td>[result]</td></tr>
<tr>
<td>arg2</td>
<td>...</td></tr>
<tr>
<td>...</td>
<td>...</td></tr>
<tr>
<td>argN</td>
<td>...</td></tr>
<tr>
<td>objectref</td>
<td>...</td></tr>
<tr>
<td>...</td>
<td>...</td></tr>
</table></dd><dt>Bytecode</dt><dd><table xmlns="">
<tr>
<td>
<b> Type </b>
</td>
<td><b>Description</b></td></tr>
<tr>
<td>
u1 
</td>
<td>invokevirtual
opcode = 0xB6 (182)
</td></tr>
<tr>
<td>
u2 
</td>
<td>index</td></tr>
</table></dd></dl></body></html>